package me.edzh.spring.bootstrap.utils;

import com.yhb.conf.ResultCode;
import me.edzh.spring.bootstrap.common.GlobalPropertiesConfigurer;
import me.edzh.spring.bootstrap.utils.exception.ServiceException;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.log4j.MDC;

public class Constants {
    public final static String KEY_REQUEST_USER_ID = "me.edzh.spring.bootstrap.requestUserId";

    public final static String JWT_KEY_USER_ID = "me.edzh.spring.bootstrap.jwt.userId";

    public final static String JWT_KEY_USER_PRIVILEGES = "me.edzh.spring.bootstrap.jwt.userPrivileges";

    public static final String LOG_TRACE_ID = "traceid";
    public static final String REMOTE_IP = "remoteip";
    public static final String REQ_START_TIME = "requestStartTime";

    public static final String DEVICE_ID = "deviceId";
    public static final String GPS = "gps";
    public static final String LOCALIZATION = "localization";
    public static final String DEVICE_TYPE = "deviceType";
    public static final String REQ_HANDLER = "reqHandlerName";

    public static final String LOG_USER_ID = "userId";

    private static Boolean isInDebug;

    public static boolean isDebug() {
        if (isInDebug == null) {
            isInDebug = "true".equals(GlobalPropertiesConfigurer.getProperty("config.isDebug"));
        }
        return isInDebug;
    }

    public static void setCurrentRemoteIp(String ip) {
        MDC.put(Constants.REMOTE_IP, ip);
    }

    public static String getCurrentRemoteIp() {
        return ObjectUtils.defaultIfNull(MDC.get(REMOTE_IP), "47.106.25.229").toString();
    }

    public static String getCurrentGPSOrError() {
        String gps = getCurrentGPS();
        if (gps == null) {
            throw new ServiceException(com.yhb.conf.ResultCode.ERROR_REQUIRE_GPS, "请提供正确的GPS信息以匹配商户");
        }

        return gps;
    }

    public static String getCurrentHandlerName() {
        return getMDC(REQ_HANDLER);
    }

    public static String getCurrentGPS() {
        return getMDC(GPS);

    }


    public static String getCurrentDeviceIdOrError() {
        String deviceId = getCurrentDeviceId();
        if (deviceId == null) {
            throw new ServiceException(com.yhb.conf.ResultCode.ERROR_REQUIRE_GPS, "请提供正确的设备信息以匹配商户");
        }

        return deviceId;
    }

    public static String getCurrentDeviceId() {
        return getMDC(DEVICE_ID);
    }

    public static String getLocalization() {
        return getMDC(LOCALIZATION);
    }

    public static String getLocalizationOrError() {
        String deviceType = getLocalization();
        if (deviceType == null) {
            throw new ServiceException(ResultCode.ERROR_REQUIRE_GPS, "请提供正确的定位信息以匹配商户");
        }

        return deviceType;
    }

    public static String getCurrentDeviceTypeOrError() {
        String deviceType = getCurrentDeviceType();
        if (deviceType == null) {
            throw new ServiceException(ResultCode.ERROR_REQUIRE_GPS, "请提供正确的设备类型以匹配商户");
        }

        return deviceType;
    }

    public static String getCurrentDeviceType() {
        return getMDC(DEVICE_TYPE);
    }


    private static String getMDC(String key) {
        Object value = MDC.get(key);
        if (value instanceof String) {
            return (String) value;
        }

        return null;

    }
}
